//
//  UIViewController+PartPop.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "UIViewController+PartPop.h"

static NSString  *THPopMaskViewStr            = @"THPopMaskViewStr";
static NSString  *THPopMasklyerAlphaStr       = @"THPopMasklyerAlphaStr";
static NSString  *THPopRemoveWhenClosedStr    = @"THPopRemoveWhenClosedStr";
static NSString  *THPopCloseAnimationTimeStr  = @"THPopCloseAnimationTimeStr";
static NSString  *THPopAppearAnimationTimeStr = @"THPopAppearAnimationTimeStr";
static NSString  *THPopIsCanNotCloseStr       = @"THPopIsCanNotCloseStr";
static NSString  *THPopCloseToWhereStr        = @"THPopCloseToWhereStr";

@implementation UIViewController (PartPop)

/**
 //默认从底部出现 滑动到底部
  @param done 回调
 */
- (void)thImg_show_ContentViewDone:(void(^)(void))done
{
    [self thImg_show_ContentViewFrom:ContentTargetTo_Bottom done:done];
}

/**
 滑出视图
 @param fromWhere 从哪里来
 @param done 回调
 */
- (void)thImg_show_ContentViewFrom:(ContentTargetTo)fromWhere done:(void(^)(void))done
{
    CGFloat offset_w = self.view.frame.size.width;
    CGFloat offset_h = self.view.frame.size.height;
    CGRect fromRect;
    switch (fromWhere) {//从哪个方向弹出
        case ContentTargetTo_Top:
            fromRect = CGRectMake(0, -offset_h, offset_w, offset_h);
            break;
        case ContentTargetTo_Bottom:
            fromRect = CGRectMake(0, offset_h, offset_w, offset_h);
            break;
        case ContentTargetTo_Left:
            fromRect = CGRectMake(-offset_w, 0, offset_w, offset_h);
            break;
        case ContentTargetTo_Right:
            fromRect = CGRectMake(offset_w, 0, offset_w, offset_h);
            break;
        default:
            fromRect = CGRectMake(0, 0, offset_w, offset_h);
            break;
    }
    self.view.frame = fromRect;
    self.zh_closeToWhere = fromWhere;
    
    [UIView animateWithDuration:self.zh_appearAnimationTime animations:^{
        self.view.frame = CGRectMake(0, 0, offset_w, offset_h);
    } completion:^(BOOL finished) {
        self.zh_maskLayerView.hidden = NO;
        if (done) {
            done();
        }
    }];
}
/**
 隐藏当前视图的动画
 @param done 回调
 */
- (void)zh_closeContentViewDone:(nullable void(^)(void))done
{
    [self zh_closeContentViewToWhere:self.zh_closeToWhere done:done];
}
/**
 隐藏当前视图的动画
 @param toWhere 往哪个方向隐藏
 @param done 回调
 */
- (void)zh_closeContentViewToWhere:(ContentTargetTo)toWhere done:(nullable void (^)(void))done
{
    CGFloat offset_w = self.view.frame.size.width;
    CGFloat offset_h = self.view.frame.size.height;
    CGRect toRect;
    switch (toWhere) {//从哪个方向隐藏
        case ContentTargetTo_Top:
            toRect = CGRectMake(0, -offset_h, offset_w, offset_h);
            break;
        case ContentTargetTo_Bottom:
            toRect = CGRectMake(0, offset_h, offset_w, offset_h);
            break;
        case ContentTargetTo_Left:
            toRect = CGRectMake(-offset_w, 0, offset_w, offset_h);
            break;
        case ContentTargetTo_Right:
            toRect = CGRectMake(offset_w, 0, offset_w, offset_h);
            break;
        default:
            toRect = CGRectMake(0, 0, offset_w, offset_h);
            break;
    }
    [UIView animateWithDuration:self.zh_closeAnimationTime animations:^{
        self.view.frame = CGRectMake(0, 0, offset_w, offset_h);
    } completion:^(BOOL finished) {
        self.zh_maskLayerView.hidden = YES;
        if (done) {
            done();
        }
    }];
}

#pragma mark - 利用runtime 手动注入属性

- (void)setzh_masklyerAlpha:(CGFloat)zh_masklyerAlpha
{
    objc_setAssociatedObject(self,&THPopMasklyerAlphaStr, @(zh_masklyerAlpha),OBJC_ASSOCIATION_ASSIGN);
}

- (void)setzh_removeWhenClosed:(BOOL)zh_removeWhenClosed
{
    objc_setAssociatedObject(self,&THPopRemoveWhenClosedStr, @(zh_removeWhenClosed),OBJC_ASSOCIATION_ASSIGN);
}

- (void)setzh_closeAnimationTime:(CGFloat)zh_closeAnimationTime
{
    objc_setAssociatedObject(self,&THPopCloseAnimationTimeStr, @(zh_closeAnimationTime),OBJC_ASSOCIATION_ASSIGN);
}

- (void)setzh_appearAnimationTime:(CGFloat)zh_appearAnimationTime
{
    objc_setAssociatedObject(self,&THPopAppearAnimationTimeStr, @(zh_appearAnimationTime),OBJC_ASSOCIATION_ASSIGN);
}

- (CGFloat)zh_appearAnimationTime
{
    return [objc_getAssociatedObject(self, &THPopAppearAnimationTimeStr) floatValue];
}

- (CGFloat)zh_closeAnimationTime
{
    return [objc_getAssociatedObject(self, &THPopCloseAnimationTimeStr) floatValue];
}

- (CGFloat)zh_masklyerAlpha
{
    return [objc_getAssociatedObject(self, &THPopMasklyerAlphaStr) floatValue];
}

- (BOOL)zh_removeWhenClosed
{
    return [objc_getAssociatedObject(self, &THPopRemoveWhenClosedStr) boolValue];
}

- (void)setzh_maskLayerView:(UIView *)zh_maskLayerView
{
    objc_setAssociatedObject(self,&THPopMaskViewStr, zh_maskLayerView,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)zh_maskLayerView
{
    UIView *maskLayerView = objc_getAssociatedObject(self, &THPopMaskViewStr);
    if (!maskLayerView) {
        maskLayerView = [[UIView alloc]init];
        maskLayerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        maskLayerView.backgroundColor = RGBA(0, 0, 0, self.zh_masklyerAlpha);
        maskLayerView.userInteractionEnabled = !self.zh_isCanNotClose;
        maskLayerView.hidden = YES;
        [maskLayerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zh_closeContentViewDone:)]];
        [self.view insertSubview:maskLayerView atIndex:0];
        objc_setAssociatedObject(self,&THPopMaskViewStr, maskLayerView,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return maskLayerView;
}

- (void)setzh_isCanNotClose:(BOOL)zh_isCanNotClose
{
    objc_setAssociatedObject(self, &THPopIsCanNotCloseStr, @(zh_isCanNotClose), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)zh_isCanNotClose
{
    return [objc_getAssociatedObject(self, &THPopIsCanNotCloseStr) boolValue];
}

- (void)setzh_closeToWhere:(ContentTargetTo)zh_closeToWhere
{
    objc_setAssociatedObject(self, &THPopCloseToWhereStr, @(zh_closeToWhere), OBJC_ASSOCIATION_ASSIGN);
}

- (ContentTargetTo)zh_closeToWhere
{
    return (ContentTargetTo)[objc_getAssociatedObject(self, &THPopIsCanNotCloseStr) integerValue];
}

@end
